package com.hb.trade.server.project.deal.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hb.core.pojo.enums.CandidateStatusEnum;
import com.hb.core.pojo.enums.DataFormFlagEnum;
import com.hb.core.pojo.parent.PageResult;
import com.hb.core.pojo.project.deal.dto.BidCandidateDTO;
import com.hb.core.pojo.project.deal.dto.BidCandidateSectionDTO;
import com.hb.core.pojo.project.deal.entity.BidCandidate;
import com.hb.core.pojo.project.deal.entity.BidCandidateSection;
import com.hb.core.pojo.project.deal.entity.BidCandidateSupplier;
import com.hb.core.pojo.project.deal.mapper.BidCandidateMapper;
import com.hb.core.pojo.project.evaluation.mapper.EvaluationThirdMapper;
import com.hb.core.pojo.project.opening.dto.OpeningBidTenderDTO;
import com.hb.trade.server.project.deal.service.BidCandidateSectionService;
import com.hb.trade.server.project.deal.service.BidCandidateService;
import com.hb.trade.server.project.deal.service.BidCandidateSupplierService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 中标候选人Service实现类
 *
 * @author Lix1ang
 * @date 2021-12-24 14:04:37
 */
@Service
public class BidCandidateServiceImpl extends ServiceImpl<BidCandidateMapper, BidCandidate> implements BidCandidateService {

    protected final EvaluationThirdMapper evaluationThirdMapper;
    protected final BidCandidateSectionService bidCandidateSectionService;
    protected final BidCandidateSupplierService bidCandidateSupplierService;

    public BidCandidateServiceImpl(EvaluationThirdMapper evaluationThirdMapper, BidCandidateSectionService bidCandidateSectionService, BidCandidateSupplierService bidCandidateSupplierService) {
        this.evaluationThirdMapper = evaluationThirdMapper;
        this.bidCandidateSectionService = bidCandidateSectionService;
        this.bidCandidateSupplierService = bidCandidateSupplierService;
    }

    /**
     *
     * @Description: 查询标段最后一次发布中标候选人公示的主键id
     * @Author: zhongdl
     * @Date:2022/1/5 16:02
     * @param sectionId:
     * @return: {@link Long }
     **/
    @Override
    public Long getLastBidCandidateId(Long sectionId) {
        return baseMapper.getLastBidCandidateId(sectionId);
    }

    /**
     *
     * @Description: 查询标段是否发布过中标候选人公示
     * @Author: zhongdl
     * @Date:2022/1/14 16:50
     * @param sectionId:
     * @return: {@link Long }
     **/
    @Override
    public boolean releaseFlagBySectionId(Long sectionId) {
        if (sectionId == null) {
            return false;
        }
        return CollUtil.isNotEmpty(baseMapper.listBidCandidate(sectionId));
    }

    @Override
    public PageResult<OpeningBidTenderDTO> getCandidate(BidCandidateSupplier bidCandidateSupplier) {
        bidCandidateSupplier.setPageSize(99);
        Page<OpeningBidTenderDTO> page = baseMapper.getCandidate(bidCandidateSupplier.pages(), bidCandidateSupplier);

        List<OpeningBidTenderDTO> openingBidTenderDTOList = new ArrayList<>();
        if (CollUtil.isNotEmpty(page.getRecords())) {
            // 判断当前标段是简易流程还是标准流程 如果是标准流程有可能会出现淘汰的企业
            for (OpeningBidTenderDTO openingBidTenderDTO : page.getRecords()) {
                // 查询公司是否淘汰
                Integer num = evaluationThirdMapper.countBySectionIdAndOrgIdAndEliminateFlag(bidCandidateSupplier.getSectionId(), openingBidTenderDTO.getOrgId());
                if (num == 0) {
                    openingBidTenderDTOList.add(openingBidTenderDTO);
                }
            }
            page.setRecords(openingBidTenderDTOList);
        }
        return PageResult.get(page);
    }

    /**
     *
     * @Description:查询需要推送的中标候选人公示列表
     * @Author: zhongdl
     * @Date:2022/2/23 16:23
     * @return: {@link List< BidCandidateDTO> }
     **/
    @Override
    public List<BidCandidateDTO> listPushBidCandidateDTO() {
        return baseMapper.listPushBidCandidateDTO();
    }

    /**
     * 根据id查询中标候选人详情
     *
     * @author yuezheng
     * @param id
     * @return com.hb.core.pojo.project.deal.dto.BidCandidateDTO
     * @date 2022/5/7 14:49
     */
    @Override
    public BidCandidateDTO getBidCandidateDTOById(Long id) {
        return baseMapper.getBidCandidateDTOById(id);
    }

    /**
     * @Description: 新增中标候选人公示
     *
     * @author yuezheng
     * @param bidCandidateDTO
     * @return java.lang.Boolean
     * @date 2022/6/25 10:33
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean addCandidate(BidCandidateDTO bidCandidateDTO) {
        //新增操作，构造基础数据
        List<BidCandidateSectionDTO> candidateSectionList = bidCandidateDTO.getCandidateSectionList();
        bidCandidateDTO.setStatus(CandidateStatusEnum.SAVE.getCode());
        bidCandidateDTO.buildBaseData(1);
        //处理标段关联表数据
        for (BidCandidateSectionDTO bidCandidateSection : candidateSectionList) {
            bidCandidateSection.setSectionId(bidCandidateSection.getId());
            bidCandidateSection.setId(null);
            bidCandidateSection.setCandidateId(bidCandidateDTO.getId());
            bidCandidateSection.buildBaseData(1);
            this.bidCandidateSectionService.save(bidCandidateSection);
            //处理候选人关联表数据
            List<BidCandidateSupplier> candidateSupplierList = bidCandidateSection.getCandidateSupplierList();
            if (CollUtil.isEmpty(candidateSupplierList)) {
                return false;
            }
            for (BidCandidateSupplier bidCandidateSupplier : candidateSupplierList) {
                bidCandidateSupplier.setId(null);
                bidCandidateSupplier.setCandidateId(bidCandidateDTO.getId());
                bidCandidateSupplier.setSectionId(bidCandidateSection.getSectionId());
                bidCandidateSupplier.buildBaseData(1);
            }
            bidCandidateSupplierService.saveBatch(candidateSupplierList);
        }
        Boolean insert = this.saveOrUpdate(bidCandidateDTO);
        return insert;
    }

    /**
     * @Description: 修改中标候选人公示
     *
     * @author yuezheng
     * @param bidCandidateDTO
     * @return java.lang.Boolean
     * @date 2022/6/25 10:35
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean updateCandidate(BidCandidateDTO bidCandidateDTO) {
        Long candidateId = bidCandidateDTO.getId();
        Integer dataFormFlag = bidCandidateDTO.getDataFormFlag();
        List<BidCandidateSectionDTO> candidateSectionList = bidCandidateDTO.getCandidateSectionList();

        if (DataFormFlagEnum.ONE.getCode() == dataFormFlag) {
            //第一步修改操作，需要对标段关联表及候选人表数据先删除后插入
            //处理标段关联表数据
            LambdaQueryWrapper<BidCandidateSection> sectionQueryWrapper = new LambdaQueryWrapper<>();
            sectionQueryWrapper.eq(BidCandidateSection::getCandidateId, candidateId);
            bidCandidateSectionService.remove(sectionQueryWrapper);

            for (BidCandidateSectionDTO bidCandidateSection : candidateSectionList) {
                bidCandidateSection.setSectionId(bidCandidateSection.getId());
                bidCandidateSection.setId(null);
                bidCandidateSection.setCandidateId(candidateId);
                bidCandidateSection.buildBaseData(1);
                bidCandidateSectionService.save(bidCandidateSection);
                //处理候选人关联表数据
                LambdaQueryWrapper<BidCandidateSupplier> supplierQueryWrapper = new LambdaQueryWrapper<>();
                supplierQueryWrapper.eq(BidCandidateSupplier::getCandidateId, candidateId)
                        .eq(BidCandidateSupplier::getSectionId, bidCandidateSection.getSectionId());
                bidCandidateSupplierService.remove(supplierQueryWrapper);

                List<BidCandidateSupplier> candidateSupplierList = bidCandidateSection.getCandidateSupplierList();
                if (CollUtil.isEmpty(candidateSupplierList)) {
                    return false;
                }
                // 收集中标候选人
                for (BidCandidateSupplier bidCandidateSupplier : candidateSupplierList) {
                    bidCandidateSupplier.setId(null);
                    bidCandidateSupplier.setCandidateId(candidateId);
                    bidCandidateSupplier.setSectionId(bidCandidateSection.getSectionId());
                    bidCandidateSupplier.buildBaseData(1);
                }
                bidCandidateSupplierService.saveBatch(candidateSupplierList);
            }
        }
        if (DataFormFlagEnum.TWO.getCode() == dataFormFlag && CollUtil.isNotEmpty(bidCandidateDTO.getPublicityContentSignature())) {
            bidCandidateDTO.setStatus(dataFormFlag);
        }
        Boolean update = this.saveOrUpdate(bidCandidateDTO);
        return update;
    }

    /**
     * @Description: 分页查询中标候选人公示
     *
     * @author yuezheng
     * @param bidCandidateDTO
     * @return com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.hb.core.pojo.project.deal.entity.BidCandidate>
     * @date 2022/6/27 08:45
     */
    @Override
    public Page<BidCandidate> findCandidatePage(BidCandidateDTO bidCandidateDTO) {
        LambdaQueryWrapper<BidCandidate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BidCandidate::getProjectId, bidCandidateDTO.getProjectId()).orderByDesc(BidCandidate::getDeployTime);
        return this.baseMapper.selectPage(bidCandidateDTO.pages(), queryWrapper);
    }
}
